//! @Author: DengLibin
//! @Date: Create in 2024-07-24 10:43:17
//! @Description:

mod test {
    use std::sync::Arc;

    use rust_my_tools::{
        component::comp_redis::{
            create_redis_client, del_val, get_multiplexed_async_connection, get_obj_value,
            get_string_value, set_obj_value, set_string_value,
        },
        entity::user::User,
    };
    use tokio::{runtime::Builder, sync::Mutex};

    pub fn cre_runtime() -> tokio::runtime::Runtime {
        // 创建一个 Tokio 运行时，配置线程池的大小
        Builder::new_multi_thread()
            // .worker_threads(4)  // 设置工作线程的数量
            .max_blocking_threads(2) // 设置阻塞任务的最大线程数
            .enable_all()
            .build()
            .unwrap()
    }

    pub async fn get_conn() -> Arc<Mutex<redis::aio::MultiplexedConnection>> {
        let client = create_redis_client("redis://192.168.6.125/").await.unwrap();
        let conn = get_multiplexed_async_connection(&client).await.unwrap();
        let a_conn = Arc::new(Mutex::new(conn));
        a_conn
    }

    #[test]
    pub fn test_set_value() {
        let r = cre_runtime();
        r.block_on(async {
            let a_conn = get_conn().await;
            set_string_value("hello_from_rust", "hello_from_rust", a_conn.clone())
                .await
                .unwrap();
        })
    }
    #[test]
    pub fn test_get_value() {
        let r = cre_runtime();
        r.block_on(async {
            let a_conn = get_conn().await;
            let r = get_string_value("hello_from_rust", a_conn.clone())
                .await
                .unwrap();
            println!("结果:{}", r);
        })
    }

    #[test]
    pub fn test_set_obj_value() {
        let r = cre_runtime();
        r.block_on(async {
            let a_conn = get_conn().await;
            let user_str = r#"{"name":"张三","id":23424234, "update_time":"2023-04-23 23:23:23"}"#;
            let u = serde_json::from_str::<User>(&user_str).unwrap();
            println!("user:{:?}", u);

            set_obj_value("rust_user", u, a_conn.clone()).await.unwrap();
        })
    }

    #[test]
    pub fn test_get_obj_value() {
        let r = cre_runtime();
        r.block_on(async {
            let a_conn = get_conn().await;
            let r = get_obj_value::<User>("rust_user", a_conn.clone())
                .await
                .unwrap();
            println!("结果:{}", serde_json::to_string(&r).unwrap());
        })
    }
    #[test]
    pub fn test_del_value() {
        let r = cre_runtime();
        r.block_on(async {
            let a_conn = get_conn().await;
            del_val("rust_user", a_conn.clone()).await.unwrap();
            
        })
    }
}
